# Copyright 2015 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

declare_args() {
  # The minimum runtime iOS version that built products are expected to run
  # on.  If empty, the toolchain will choose its own default, typically the
  # most recent OS version.
  ios_deployment_target = "14.0"

  # SDK path to use. When empty this will use the default SDK based on the
  # value of use_ios_simulator.
  ios_sdk_path = ""

  # Prefix for CFBundleIdentifier property of iOS bundles (correspond to the
  # "Organization Identifier" in Xcode). Code signing will fail if no mobile
  # provisioning for the selected code signing identify support that prefix.
  ios_app_bundle_id_prefix = "org.chromium"

  # The iOS Code signing identity to use
  ios_enable_code_signing = true
  ios_code_signing_identity = ""
  ios_code_signing_identity_description = "Apple Development"

  # Configure the environment for which to build. Could be either "device",
  # "simulator" or "catalyst". If unspecified, then it will be assumed to be
  # "simulator" if the target_cpu is "x68" or "x64", "device" otherwise. The
  # default is only there for compatibility reasons and will be removed (see
  # crbug.com/1138425 for more details).
  target_environment = ""
}

if (target_environment == "") {
  if (current_cpu == "x86" || current_cpu == "x64") {
    target_environment = "simulator"
  } else {
    target_environment = "device"
  }
}

use_ios_simulator = target_environment == "simulator"

if (ios_sdk_path == "") {
  # Compute default target.
  if (use_ios_simulator) {
    ios_sdk_name = "iphonesimulator"
    ios_sdk_platform = "iPhoneSimulator"
  } else {
    ios_sdk_name = "iphoneos"
    ios_sdk_platform = "iPhoneOS"
  }

  ios_sdk_info_args = [ "--get_sdk_info" ]
  ios_sdk_info_args += [ ios_sdk_name ]
  _ios_sdk_result = exec_script("sdk_info.py", ios_sdk_info_args, "scope")
  ios_sdk_path = _ios_sdk_result.sdk_path
  ios_sdk_version = _ios_sdk_result.sdk_version
  ios_sdk_platform_path = _ios_sdk_result.sdk_platform_path
  ios_sdk_build = _ios_sdk_result.sdk_build
  xcode_version = _ios_sdk_result.xcode_version
  xcode_version_int = _ios_sdk_result.xcode_version_int
  xcode_build = _ios_sdk_result.xcode_build
  machine_os_build = _ios_sdk_result.machine_os_build
  if (use_ios_simulator) {
    # This is weird, but Xcode sets DTPlatformBuild to an empty field for
    # simulator builds.
    ios_platform_build = ""
  } else {
    ios_platform_build = ios_sdk_build
  }
}

if (ios_enable_code_signing && !use_ios_simulator) {
  find_signing_identity_args = [
    "--identity-description",
    ios_code_signing_identity_description,
  ]

  # If an identity is not provided, look for one on the host
  if (ios_code_signing_identity == "") {
    _ios_identities = exec_script("find_signing_identity.py",
                                  find_signing_identity_args,
                                  "list lines")
    if (_ios_identities == []) {
      print("Automatic code signing identity selection was enabled but could")
      print("not find exactly one code signing identity matching")
      print("$ios_code_signing_identity_description. Check that your keychain")
      print("is accessible and that there is a valid code signing identity")
      print("listed by `xcrun security find-identity -v -p codesigning`")
      print("TIP: Simulator builds don't require code signing...")
      assert(false)
    } else {
      _ios_identities_len = 0
      foreach(_, _ios_identities) {
        _ios_identities_len += 1
      }

      ios_code_signing_identity = _ios_identities[0]
      if (_ios_identities_len != 1) {
        print("Warning: Multiple codesigning identities match " +
              "\"$ios_code_signing_identity_description\"")
        foreach(_ios_identity, _ios_identities) {
          _selected = ""
          if (ios_code_signing_identity == _ios_identity) {
            _selected = " (selected)"
          }
          print("Warning: - $_ios_identity$_selected")
        }
        print("Warning: Please use either ios_code_signing_identity or ")
        print("Warning: ios_code_signing_identity_description variable to ")
        print("Warning: control which identity is selected.")
        print()
      }
    }
  }
}
